package com.ydkj.a37e_mall.Classes.Fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.foamtrace.photopicker.Image;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.ydkj.a37e_mall.Classes.Acitivity.BaseActivity.CommodityDetails;
import com.ydkj.a37e_mall.Classes.Model.Request.ProductcomplexReq;
import com.ydkj.a37e_mall.Classes.Model.Response.ProductcomplexResp;
import com.ydkj.a37e_mall.R;
import com.ydkj.a37e_mall.Resource.Utils.Async_http_get;
import com.ydkj.a37e_mall.Resource.Utils.CommonUtil;
import com.ydkj.a37e_mall.Resource.Utils.Const;
import com.ydkj.a37e_mall.Resource.Utils.GsonImpl;
import com.ydkj.a37e_mall.Resource.Utils.SharedPreferencesUtil;
import com.ydkj.a37e_mall.Resource.View.CustomProgressDialog;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cz.msebera.android.httpclient.Header;

import static com.ydkj.a37e_mall.R.id.img_declare_commodity1;
import static com.ydkj.a37e_mall.R.id.img_declare_commodity_name;

/**
 * Created by Administrator on 2017/1/13.
 * 申报商品
 */

public class DeclareCommodityFg extends BaseFg {
    private View rootView;
    private Activity mActivity;
    private PullToRefreshListView ref_declare_commodity;
    private DeclareCommodityAdapter declareCommodityAdapter;
    private List<ProductcomplexResp.ResultEntity.DataEntity> productlist;
    private SharedPreferencesUtil sharedPreferencesUtil;
    private GvDeclareAdapter gvDeclareAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.declare_commodity, container, false);
        }
        ViewGroup parent = (ViewGroup) rootView.getParent();
        if (parent != null) {
            parent.removeView(rootView);
        }

        return rootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mActivity = getActivity();
        init();
    }

    private void init() {

        sharedPreferencesUtil = SharedPreferencesUtil.getInstance(mActivity);


        productlist = new ArrayList<>();

        ref_declare_commodity = (PullToRefreshListView) mActivity.findViewById(R.id.ref_declare_commodity);

        ref_declare_commodity.setMode(PullToRefreshBase.Mode.PULL_FROM_START);

        declareCommodityAdapter = new DeclareCommodityAdapter(productlist);

        ref_declare_commodity.setAdapter(declareCommodityAdapter);


        ref_declare_commodity.setOnRefreshListener2(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                GetProductcomplex();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {

            }
        });
        GetProductcomplex();
    }

    private class DeclareCommodityAdapter extends BaseAdapter {
        private List<ProductcomplexResp.ResultEntity.DataEntity> productlist;

        private DeclareCommodityAdapter(List<ProductcomplexResp.ResultEntity.DataEntity> info) {
            if (info == null) {
                info = new ArrayList<>();
            }
            productlist = info;
        }

        @Override
        public int getCount() {
            return productlist.size();
        }

        @Override
        public ProductcomplexResp.ResultEntity.DataEntity getItem(int position) {
            return productlist.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertview, ViewGroup viewGroup) {

            ViewHolder vh = null;
            if (convertview == null) {
                convertview = LayoutInflater.from(mActivity).inflate(R.layout.declare_commodity_list, null);
                vh = new ViewHolder();
                vh.rly_declare_commodity = (RelativeLayout) convertview.findViewById(R.id.rly_declare_commodity);
                vh.img_declare_commodity_name = (TextView) convertview.findViewById(img_declare_commodity_name);

                vh.img_declare_commodity1 = (ImageView) convertview.findViewById(img_declare_commodity1);
                vh.img_declare_commodity2 = (ImageView) convertview.findViewById(R.id.img_declare_commodity2);
                vh.img_declare_commodity3 = (ImageView) convertview.findViewById(R.id.img_declare_commodity3);
                vh.img_declare_commodity4 = (ImageView) convertview.findViewById(R.id.img_declare_commodity4);
                vh.img_declare_commodity5 = (ImageView) convertview.findViewById(R.id.img_declare_commodity5);
                vh.img_declare_commodity6 = (ImageView) convertview.findViewById(R.id.img_declare_commodity6);
                vh.img_declare_commodity7 = (ImageView) convertview.findViewById(R.id.img_declare_commodity7);
                vh.img_declare_commodity8 = (ImageView) convertview.findViewById(R.id.img_declare_commodity8);

                vh.tv_declare_commodity1_1 = (TextView) convertview.findViewById(R.id.tv_declare_commodity1_1);
                vh.tv_declare_commodity2_1 = (TextView) convertview.findViewById(R.id.tv_declare_commodity2_1);
                vh.tv_declare_commodity3_1 = (TextView) convertview.findViewById(R.id.tv_declare_commodity3_1);
                vh.tv_declare_commodity4_1 = (TextView) convertview.findViewById(R.id.tv_declare_commodity4_1);
                vh.tv_declare_commodity5_1 = (TextView) convertview.findViewById(R.id.tv_declare_commodity5_1);
                vh.tv_declare_commodity6_1 = (TextView) convertview.findViewById(R.id.tv_declare_commodity6_1);
                vh.tv_declare_commodity7_1 = (TextView) convertview.findViewById(R.id.tv_declare_commodity7_1);
                vh.tv_declare_commodity8_1 = (TextView) convertview.findViewById(R.id.tv_declare_commodity8_1);

                vh.tv_declare_commodity1_2 = (TextView) convertview.findViewById(R.id.tv_declare_commodity1_2);
                vh.tv_declare_commodity2_2 = (TextView) convertview.findViewById(R.id.tv_declare_commodity2_2);
                vh.tv_declare_commodity3_2 = (TextView) convertview.findViewById(R.id.tv_declare_commodity3_2);
                vh.tv_declare_commodity4_2 = (TextView) convertview.findViewById(R.id.tv_declare_commodity4_2);
                vh.tv_declare_commodity5_2 = (TextView) convertview.findViewById(R.id.tv_declare_commodity5_2);
                vh.tv_declare_commodity6_2 = (TextView) convertview.findViewById(R.id.tv_declare_commodity6_2);
                vh.tv_declare_commodity7_2 = (TextView) convertview.findViewById(R.id.tv_declare_commodity7_2);
                vh.tv_declare_commodity8_2 = (TextView) convertview.findViewById(R.id.tv_declare_commodity8_2);

                vh.lly_declare_commodity1 = (LinearLayout) convertview.findViewById(R.id.lly_declare_commodity1);
                vh.lly_declare_commodity2 = (LinearLayout) convertview.findViewById(R.id.lly_declare_commodity2);
                vh.lly_declare_commodity3 = (LinearLayout) convertview.findViewById(R.id.lly_declare_commodity3);
                vh.lly_declare_commodity4 = (LinearLayout) convertview.findViewById(R.id.lly_declare_commodity4);
                vh.lly_declare_commodity5 = (LinearLayout) convertview.findViewById(R.id.lly_declare_commodity5);
                vh.lly_declare_commodity6 = (LinearLayout) convertview.findViewById(R.id.lly_declare_commodity6);
                vh.lly_declare_commodity7 = (LinearLayout) convertview.findViewById(R.id.lly_declare_commodity7);
                vh.lly_declare_commodity8 = (LinearLayout) convertview.findViewById(R.id.lly_declare_commodity8);

                vh.gv_declare_commodity = (GridView) convertview.findViewById(R.id.gv_declare_commodity);

            } else {
                vh = (ViewHolder) convertview.getTag();
            }
            convertview.setTag(vh);

            vh.img_declare_commodity_name.setText(getItem(position).getName());
            //标题栏
            vh.rly_declare_commodity.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                }
            });

            gvDeclareAdapter = new GvDeclareAdapter(getItem(position).getProduces());
            vh.gv_declare_commodity.setAdapter(gvDeclareAdapter);


//            vh.tv_declare_commodity1_1.setText(getItem(position).getProduces().get(0).getName());
//            vh.tv_declare_commodity2_1.setText(getItem(position).getProduces().get(1).getName());
//            vh.tv_declare_commodity3_1.setText(getItem(position).getProduces().get(2).getName());
//            vh.tv_declare_commodity4_1.setText(getItem(position).getProduces().get(3).getName());
//            vh.tv_declare_commodity5_1.setText(getItem(position).getProduces().get(4).getName());
//            vh.tv_declare_commodity6_1.setText(getItem(position).getProduces().get(5).getName());
//            vh.tv_declare_commodity7_1.setText(getItem(position).getProduces().get(6).getName());
//            vh.tv_declare_commodity8_1.setText(getItem(position).getProduces().get(7).getName());

//            vh.tv_declare_commodity1_2.setText(getItem(position).getProduces().get(0).getMarket_price() + "元");
//            vh.tv_declare_commodity2_2.setText(getItem(position).getProduces().get(1).getMarket_price() + "元");
//            vh.tv_declare_commodity3_2.setText(getItem(position).getProduces().get(2).getMarket_price() + "元");
//            vh.tv_declare_commodity4_2.setText(getItem(position).getProduces().get(3).getMarket_price() + "元");
//            vh.tv_declare_commodity5_2.setText(getItem(position).getProduces().get(4).getMarket_price() + "元");
//            vh.tv_declare_commodity6_2.setText(getItem(position).getProduces().get(5).getMarket_price() + "元");
//            vh.tv_declare_commodity7_2.setText(getItem(position).getProduces().get(6).getMarket_price() + "元");
//            vh.tv_declare_commodity8_2.setText(getItem(position).getProduces().get(7).getMarket_price() + "元");


//            ImageLoader.getInstance().displayImage(getItem(position).getProduces().get(0).getList_thumb(), vh.img_declare_commodity1, displayImageOptions);
//            ImageLoader.getInstance().displayImage(getItem(position).getProduces().get(1).getList_thumb(), vh.img_declare_commodity2, displayImageOptions);
//            ImageLoader.getInstance().displayImage(getItem(position).getProduces().get(2).getList_thumb(), vh.img_declare_commodity3, displayImageOptions);
//            ImageLoader.getInstance().displayImage(getItem(position).getProduces().get(3).getList_thumb(), vh.img_declare_commodity4, displayImageOptions);
//            ImageLoader.getInstance().displayImage(getItem(position).getProduces().get(4).getList_thumb(), vh.img_declare_commodity5, displayImageOptions);
//            ImageLoader.getInstance().displayImage(getItem(position).getProduces().get(5).getList_thumb(), vh.img_declare_commodity6, displayImageOptions);
//            ImageLoader.getInstance().displayImage(getItem(position).getProduces().get(6).getList_thumb(), vh.img_declare_commodity7, displayImageOptions);
//            ImageLoader.getInstance().displayImage(getItem(position).getProduces().get(7).getList_thumb(), vh.img_declare_commodity8, displayImageOptions);


//            vh.lly_declare_commodity1.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View view) {
//                    Intent intent = new Intent(mActivity, CommodityDetails.class);
//                    intent.putExtra("id", getItem(position).getProduces().get(0).getId());
//                    startActivity(intent);
//
//                }
//            });
//            vh.lly_declare_commodity2.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View view) {
//                    Intent intent = new Intent(mActivity, CommodityDetails.class);
//                    intent.putExtra("id", getItem(position).getProduces().get(1).getId());
//                    startActivity(intent);
//                }
//            });
//            vh.lly_declare_commodity3.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View view) {
//                    Intent intent = new Intent(mActivity, CommodityDetails.class);
//                    intent.putExtra("id", getItem(position).getProduces().get(2).getId());
//                    startActivity(intent);
//                }
//            });
//            vh.lly_declare_commodity4.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View view) {
//                    Intent intent = new Intent(mActivity, CommodityDetails.class);
//                    intent.putExtra("id", getItem(position).getProduces().get(3).getId());
//                    startActivity(intent);
//                }
//            });
//            vh.lly_declare_commodity5.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View view) {
//                    Intent intent = new Intent(mActivity, CommodityDetails.class);
//                    intent.putExtra("id", getItem(position).getProduces().get(4).getId());
//                    startActivity(intent);
//                }
//            });
//            vh.lly_declare_commodity6.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View view) {
//                    Intent intent = new Intent(mActivity, CommodityDetails.class);
//                    intent.putExtra("id", getItem(position).getProduces().get(5).getId());
//                    startActivity(intent);
//                }
//            });
//            vh.lly_declare_commodity7.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View view) {
//                    Intent intent = new Intent(mActivity, CommodityDetails.class);
//                    intent.putExtra("id", getItem(position).getProduces().get(6).getId());
//                    startActivity(intent);
//                }
//            });
//            vh.lly_declare_commodity8.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View view) {
//                    Intent intent = new Intent(mActivity, CommodityDetails.class);
//                    intent.putExtra("id", getItem(position).getProduces().get(7).getId());
//                    startActivity(intent);
//                }
//            });
            return convertview;
        }

        private class ViewHolder {
            private RelativeLayout rly_declare_commodity;
            private TextView img_declare_commodity_name;
            private ImageView img_declare_commodity1, img_declare_commodity2, img_declare_commodity3, img_declare_commodity4,
                    img_declare_commodity5, img_declare_commodity6, img_declare_commodity7, img_declare_commodity8;
            private TextView tv_declare_commodity1_1, tv_declare_commodity2_1, tv_declare_commodity3_1, tv_declare_commodity4_1,
                    tv_declare_commodity5_1, tv_declare_commodity6_1, tv_declare_commodity7_1, tv_declare_commodity8_1;
            private TextView tv_declare_commodity1_2, tv_declare_commodity2_2, tv_declare_commodity3_2, tv_declare_commodity4_2,
                    tv_declare_commodity5_2, tv_declare_commodity6_2, tv_declare_commodity7_2, tv_declare_commodity8_2;
            private LinearLayout lly_declare_commodity1, lly_declare_commodity2, lly_declare_commodity3, lly_declare_commodity4,
                    lly_declare_commodity5, lly_declare_commodity6, lly_declare_commodity7, lly_declare_commodity8;
            private GridView gv_declare_commodity;
        }

    }

    private class GvDeclareAdapter extends BaseAdapter {
        private List<ProductcomplexResp.ResultEntity.DataEntity.ProducesEntity> productlist;

        private GvDeclareAdapter(List<ProductcomplexResp.ResultEntity.DataEntity.ProducesEntity> info) {
            if (info == null) {
                info = new ArrayList<>();
            }
            productlist = info;
        }

        @Override
        public int getCount() {
            return productlist.size();
        }

        @Override
        public ProductcomplexResp.ResultEntity.DataEntity.ProducesEntity getItem(int position) {
            return productlist.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertview, ViewGroup viewGroup) {
            ViewHolder vh = null;
            if (convertview == null) {
                convertview = LayoutInflater.from(mActivity).inflate(R.layout.gv_declare_commodity_list, null);
                vh = new ViewHolder();
                vh.img_declare_commodity1 = (ImageView) convertview.findViewById(R.id.img_declare_commodity1);
                vh.tv_declare_commodity1_1 = (TextView) convertview.findViewById(R.id.tv_declare_commodity1_1);
                vh.tv_declare_commodity1_2 = (TextView) convertview.findViewById(R.id.tv_declare_commodity1_2);


            } else {
                vh = (ViewHolder) convertview.getTag();
            }
            convertview.setTag(vh);
            convertview.setClickable(true);
            ImageLoader.getInstance().displayImage(getItem(position).getList_thumb(), vh.img_declare_commodity1, displayImageOptions);
            vh.tv_declare_commodity1_1.setText(getItem(position).getName());
            vh.tv_declare_commodity1_2.setText(getItem(position).getMarket_price());


            convertview.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(mActivity, CommodityDetails.class);
                    Log.e("ssss", "申报商品_id==>" + getItem(position).getId());
                    intent.putExtra("id", getItem(position).getId());
                    startActivity(intent);
                }
            });

            return convertview;
        }

        private class ViewHolder {
            private ImageView img_declare_commodity1;
            private TextView tv_declare_commodity1_1, tv_declare_commodity1_2;

        }
    }

    //获取商品
    private void GetProductcomplex() {
        CustomProgressDialog.createDialog(mActivity).show();
        ProductcomplexReq req = new ProductcomplexReq();
        Log.e("ssss", "time==>" + System.currentTimeMillis());
        req.setTime(System.currentTimeMillis() + "");
        RequestParams params = new RequestParams();


        params.put("secret", req.getSecret());
        params.put("token", sharedPreferencesUtil.getAccessToken());
        Async_http_get.get(Const.COMMON_ADDRESS + req.urlString(), params, new JsonHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                CustomProgressDialog.Dismiss();
                ref_declare_commodity.onRefreshComplete();
                String errno = response.optString("errno");
                String errmsg = response.optString("errmsg");
                Log.e("url", "response==" + response);
                if (errno.equals("000")) {
                    ProductcomplexResp resp = GsonImpl.get().toObject(response.toString(), ProductcomplexResp.class);
                    productlist.clear();
                    productlist.addAll(resp.getResult().getData());
                    declareCommodityAdapter.notifyDataSetChanged();
                } else {
                    CommonUtil.showToast(mActivity, errmsg);
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                super.onFailure(statusCode, headers, responseString, throwable);
                CustomProgressDialog.Dismiss();
                CommonUtil.showToast(mActivity, "网络连接失败");

            }
        });
    }


}
